﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using System.Configuration;
using System.Collections;
using ClientDependency.Core;
using ClientDependency.Core.Controls;


[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.css.tabularData.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.scripts.tabularData.js", "text/javascript")]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.images.icons.png", "img/png")]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.images.title.gif", "img/gif")]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.images.info.png", "img/png")]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.images.warning.png", "img/png")]
[assembly: WebResource("MT.Flag.Umbraco.DataTypes.TabularData.images.help.png", "img/png")]




namespace MT.Flag.Umbraco.DataTypes.TabularData
{
    public class TabularDataControl : Panel
    {


        private HiddenField hdnXML = new HiddenField();
        private CheckBox chkHeaderColumn;
        private CheckBox chkHeaderRow;
        private CheckBox chkFooterRow;


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }



        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //ClientDependencyLoader.Instance.RegisterDependency(Page.ClientScript.GetWebResourceUrl(this.GetType(), "MT.Flag.Umbraco.DataTypes.TabularData.css.tabularData.css"), ClientDependencyType.Css);
            //ClientDependencyLoader.Instance.RegisterDependency(Page.ClientScript.GetWebResourceUrl(this.GetType(), "MT.Flag.Umbraco.DataTypes.TabularData.scripts.tabularData.js"), ClientDependencyType.Javascript);

            // add css and js scripts
            if (!Page.ClientScript.IsClientScriptBlockRegistered("tabularDataCss"))
            {
                string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "MT.Flag.Umbraco.DataTypes.TabularData.css.tabularData.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "tabularDataCss", css, false);
            }

            if (!Page.ClientScript.IsClientScriptBlockRegistered("tabularDataScript"))
                this.Page.ClientScript.RegisterClientScriptInclude("tabularDataScript", this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "MT.Flag.Umbraco.DataTypes.TabularData.scripts.tabularData.js"));

            // this seems to cause problems... it looks like these are called before jQuery is loaded...
            

            this.CssClass = "tabularData";
            this.ID = this.UniqueID;

            chkHeaderColumn.Text = "Header column";
            chkHeaderColumn.Checked = this.HeaderColumn;
            chkHeaderColumn.CssClass = "tabularDataHeaderColumn";

            chkHeaderRow.Text = "Header row";
            chkHeaderRow.Checked = this.HeaderRow;
            chkHeaderRow.CssClass = "tabularDataHeaderRow";
            
            chkFooterRow.Text = "Footer row";
            chkFooterRow.Checked = this.FooterRow;
            chkFooterRow.CssClass = "tabularDataFooterRow";

            Literal litResizer = new Literal();
            StringBuilder rt = new StringBuilder();
            rt.Append("<a href=\"#\" class=\"showResizer\" title=\"Resize table\"></a>");
            StringBuilder rr = new StringBuilder();
            rr.Append("<tr>");
            for (int x = 0; x < 10; x++)
                rr.Append("<td><a /></td>");
            rr.Append("</tr>");
            string rrs = rr.ToString();
            rt.Append("<div class=\"resizer\"><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody>");
            for (int x = 0; x < 10; x++)
                rt.Append(rrs);
            rt.Append("</tbody></table><span class=\"newSize\"></span></div>");
            litResizer.Text = rt.ToString();

            Panel pnlTabularDataFunctions = new Panel();
            pnlTabularDataFunctions.CssClass = "tabularDataFunctions";
            pnlTabularDataFunctions.Controls.Add(litResizer);
            pnlTabularDataFunctions.Controls.Add(chkHeaderColumn);
            pnlTabularDataFunctions.Controls.Add(chkHeaderRow);
            pnlTabularDataFunctions.Controls.Add(chkFooterRow);

            this.Controls.Add(pnlTabularDataFunctions);


            Panel pnlTable = new Panel();
            pnlTable.CssClass = "tabularDataTable";
            this.Controls.Add(pnlTable);


            if (this.Value == "")
            {
                // build default XML
                StringBuilder sb = new StringBuilder();
                if (this.Rows > 0 && this.Columns > 0)
                {

                    sb.Append("<Data>");

                    sb.Append("<Columns>");
                    if (this.HeaderRow) { sb.Append("<Column width=\"" + this.HeaderWidth.ToString() + "\" />"); }
                    for (int cw = 0; cw < this.Columns; cw++) { sb.Append("<Column width=\"" + this.DefaultWidth.ToString() + "\" />"); }
                    sb.Append("</Columns>");

                    if (this.HeaderRow)
                    {
                        sb.Append("<Header><Row>");
                        for (int i = 0; i < (this.HeaderColumn ? this.Columns + 1 : this.Columns); i++)
                            sb.Append("<ColHead />");
                        sb.Append("</Row></Header>");
                    }

                    if (this.FooterRow)
                    {
                        sb.Append("<Footer><Row>");
                        if (this.HeaderColumn) { sb.Append("<RowHead />"); }
                        for (int i = 0; i < this.Columns; i++)
                            sb.Append("<Cell />");
                        sb.Append("</Row></Footer>");
                    }

                    sb.Append("<Body>");
                    for (int r = 0; r < this.Rows; r++)
                    {
                        sb.Append("<Row>");
                        if (this.HeaderColumn) { sb.Append("<RowHead />"); }
                        for (int i = 0; i < this.Columns; i++)
                            sb.Append("<Cell />");
                        sb.Append("</Row>");
                    }
                    sb.Append("</Body>");
                    sb.Append("</Data>");

                    this.Value = sb.ToString();
                }
            }


            // hidden field for storing value
            hdnXML.Value = this.Value.ToString();
            this.Controls.Add(hdnXML);


            // context menu
            Literal litContext = new Literal();
            litContext.Text = "<ul class=\"tabularDataMenu contextMenu\"><li class=\"rowInsertAbove\"><a href=\"#rowInsertAbove\">Insert row above</a></li><li class=\"rowInsertBelow\"><a href=\"#rowInsertBelow\">Insert row below</a></li><li class=\"rowDelete\"><a href=\"#rowDelete\">Delete row</a></li><li class=\"colInsertLeft separator\"><a href=\"#colInsertLeft\">Insert column left</a></li><li class=\"colInsertRight\"><a href=\"#colInsertRight\">Insert column right</a></li><li class=\"colDelete\"><a href=\"#colDelete\">Delete column</a></li><li class=\"colWidth\"><a href=\"#colWidth\">Change column width</a></li></ul>";
            this.Controls.Add(litContext);




        }



        private int _rows;
        public int Rows
        {
            get { return _rows; }
            set { _rows = value; }
        }

        private int _cols;
        public int Columns
        {
            get { return _cols; }
            set { _cols = value; }
        }


        private int _headerWidth;
        public int HeaderWidth
        {
            get { return _headerWidth; }
            set { _headerWidth = value; }
        }

        private int _defaultWidth;
        public int DefaultWidth
        {
            get { return _defaultWidth; }
            set { _defaultWidth = value; }
        }
        


        //private bool _headerCol;
        public bool HeaderColumn
        {
            get { return chkHeaderColumn.Checked; }
            set
            {
                if (chkHeaderColumn == null)
                    chkHeaderColumn = new CheckBox();
                chkHeaderColumn.Checked = value;
            }
        }

        //private bool _headerRow;
        public bool HeaderRow
        {
            get { return chkHeaderRow.Checked; }
            set
            {
                if (chkHeaderRow == null)
                    chkHeaderRow = new CheckBox();
                chkHeaderRow.Checked = value;
            }
        }

        //private bool _footerRow;
        public bool FooterRow
        {
            get { return chkFooterRow.Checked; }
            set
            {
                if (chkFooterRow == null)
                    chkFooterRow = new CheckBox();
                chkFooterRow.Checked = value;
            }
        }
        
        
        /// <summary>
        /// stored value
        /// </summary>
        //private string _data;
        public string Value
        {
            get { return hdnXML.Value; }
            set { hdnXML.Value = value; }
        }
        
        
    }
}
